package cn.itrip.controller;

import cn.itrip.config.AlipayConfig;
import cn.itrip.service.OrderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import springfox.documentation.annotations.ApiIgnore;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;

import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.common.EmptyUtils;
import cn.itrip.config.AlipayConfig;
import cn.itrip.service.OrderService;

/**
 * 支付处理控制器
 */
@Controller
@RequestMapping("/api")
public class AliPaymentController {

	private Logger logger = Logger.getLogger(AliPaymentController.class);
	@Resource
	private AlipayConfig alipayConfig;
	@Resource
	private OrderService orderService;

	/**
	 * 确认订单信息
	 *
	 * @param orderNo 订单ID
	 * @return
	 */
	@ApiIgnore
	@RequestMapping(value = "/prepay/{orderNo}", method = RequestMethod.GET)
	public String prePay(@PathVariable String orderNo, ModelMap model) {
		try{
			ItripHotelOrder order = orderService.loadItripHotelOrder(orderNo);
			if (order!=null){
				model.addAttribute("orderNo",order.getOrderNo());
				model.addAttribute("roomId",order.getRoomId());
				model.addAttribute("count",order.getCount());
				model.addAttribute("payAmount",order.getPayAmount());
				model.addAttribute("hotelName",order.getHotelName());
				return "pay";
			}else {
				return "error";
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return "error";
	}

	/**
	 * 向支付宝提交支付请求
	 *
	 * @param WIDout_trade_no 商户订单号，商户网站订单系统中唯一订单号，必填
	 * @param WIDsubject      订单名称，必填
	 * @param WIDtotal_amount 付款金额，必填
	 */
	@ApiOperation(value = "订单支付", httpMethod = "POST", 
			protocols = "HTTP", produces = "application/xml", consumes="application/x-www-form-urlencoded",
			response =  String.class, 
			notes = "客户端提交订单支付请求，对该API的返回结果不用处理，浏览器将自动跳转至支付宝。<br><b>请使用普通表单提交，不能使用ajax异步提交。</b>")	
	@RequestMapping(value = "/pay", method = RequestMethod.POST,produces="application/xml", consumes="application/x-www-form-urlencoded")
	public void pay(
			@ApiParam(name="WIDout_trade_no",value="订单编号",required=true)
			@RequestParam String WIDout_trade_no, 
			@ApiParam(name="WIDsubject",value="订单名称",required=true)
			@RequestParam String WIDsubject,
			@ApiParam(name="WIDtotal_amount",value="订单金额",required=true)
			@RequestParam String WIDtotal_amount, HttpServletResponse response) {
		String timeout_express="2m";
		String product_code="QUICK_WAP_PAY";
		AlipayClient client = new DefaultAlipayClient(alipayConfig.getUrl(),
				alipayConfig.getAppID(),
				alipayConfig.getRsaPrivateKey(),
				alipayConfig.getFormat(),
				alipayConfig.getCharset(),
				alipayConfig.getAlipayPublicKey(),
				alipayConfig.getSignType());
		AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

		AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
		model.setOutTradeNo(WIDout_trade_no);
		model.setSubject(WIDsubject);
		model.setTotalAmount(WIDtotal_amount);
		model.setTimeoutExpress(timeout_express);
		model.setProductCode(product_code);
		alipay_request.setBizModel(model);
		// 设置异步通知地址
		alipay_request.setNotifyUrl(alipayConfig.getNotifyUrl());
		// 设置同步地址
		alipay_request.setReturnUrl(alipayConfig.getReturnUrl());

		// form表单生产
		String form = "";
		try {
			// 调用SDK生成表单
			form = client.pageExecute(alipay_request).getBody();
			response.setContentType("text/html;charset=" + alipayConfig.getCharset());
			response.getWriter().write(form);//直接将完整的表单html输出到页面
			response.getWriter().flush();
			response.getWriter().close();
		} catch (AlipayApiException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 异步通知，跟踪支付状态变更
	 */

	@ApiIgnore
	@RequestMapping(value = "/notify", method = RequestMethod.POST)
	public void trackPaymentStatus(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String,String> params = new HashMap<String,String>();
		Map requestParams = request.getParameterMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
						: valueStr + values[i] + ",";
			}
			//乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
			//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
			params.put(name, valueStr);
		}
		//获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)//
		//商户订单号

		String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
		//支付宝交易号

		String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");

		//交易状态
		String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");

		//获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//
		//计算得出通知验证结果
		//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
		boolean verify_result = AlipaySignature.rsaCheckV1(params, alipayConfig.getAlipayPublicKey(), alipayConfig.getCharset(), "RSA2");

		if(verify_result){//验证成功
			//////////////////////////////////////////////////////////////////////////////////////////
			//请在这里加上商户的业务逻辑程序代码

			//——请根据您的业务逻辑来编写程序（以下代码仅作参考）——

			if(trade_status.equals("TRADE_FINISHED")){
				this.orderService.paySuccess(out_trade_no,1,trade_no);
			} else if (trade_status.equals("TRADE_SUCCESS")){
				this.orderService.paySuccess(out_trade_no,1,trade_no);
			}
			response.getWriter().println("success");


		}else{//验证失败
			this.orderService.payFailed(out_trade_no,1,trade_no);
			response.getWriter().println("fail");
		}
	}

	/**
	 * 支付宝页面跳转同步通知页面
	 */
	@ApiIgnore
	@RequestMapping(value = "/return", method = RequestMethod.GET)
	public void callback(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String,String> params = new HashMap<String,String>();
		Map requestParams = request.getParameterMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
						: valueStr + values[i] + ",";
			}
			//乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
			valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
			params.put(name, valueStr);
		}

		//获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)//
		//商户订单号

		String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");

		//支付宝交易号

		String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");

		//获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//
		//计算得出通知验证结果
		//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
		boolean verify_result = AlipaySignature.rsaCheckV1(params, alipayConfig.getAlipayPublicKey(), alipayConfig.getCharset(), "RSA2");
		String id=orderService.loadItripHotelOrder(out_trade_no).getId().toString();
		if(verify_result){//验证成功
			//////////////////////////////////////////////////////////////////////////////////////////
			//请在这里加上商户的业务逻辑程序代码
			//该页面可做页面美工编辑
			response.sendRedirect(String.format(alipayConfig.getPaymentSuccessUrl(),trade_no,id));
			//——请根据您的业务逻辑来编写程序（以上代码仅作参考）——

			//////////////////////////////////////////////////////////////////////////////////////////
		}else{
			//该页面可做页面美工编辑
			response.sendRedirect(String.format(alipayConfig.getPaymentFailureUrl(),trade_no,id));
		}
	}
}
